/**
 * Copyright (c) 2012, David Varbel
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.epistrata.neutraltheory;

import static org.junit.Assert.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.epistrata.experiment.Run;

public class BitSetCritterTest {
	Run run;
	NeutralTheoryModel model;

	@Before
	public void setUp() throws Exception {
		HashMap<String, String> factors = new HashMap<String, String>();
		factors.put("Dispersal Distance","1");
		factors.put("Dispersal Probability","0");
		factors.put("Early Death Probability","0");
		factors.put("Genetic Distance","2");
		factors.put("Minimum Possible Potential Mates","1");
		factors.put("Mutation Probability","0.5");
		factors.put("Spacial Distance","2");
		
		List<String> responseTypes = new ArrayList<String>();
		responseTypes.add("Recent Species Mean");
		responseTypes.add("Recent Species Variance");
		
		run = new Run(null, 1, 1, factors, responseTypes);
		model = new NeutralTheoryModel(run);
		Field genomeSizeField = model.getClass().getDeclaredField("genomeSize");
		genomeSizeField.setAccessible(true);
		genomeSizeField.set(model, 8);
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testMateWith() throws Exception {
		double xLoc = 100.0d;
		double yLoc = 100.0d;
		
		BitSetCritter thisCritter = BitSetCritter.newRandomInstance(model);
		Field thisGenomeField = thisCritter.getClass().getDeclaredField("genome");
		thisGenomeField.setAccessible(true);
		BitSet thisGenome = new BitSet(8);
		thisGenome.set(4, 8, true);
		thisGenomeField.set(thisCritter, thisGenome);
		thisCritter.setLocation(xLoc, yLoc);

		BitSetCritter otherCritter = BitSetCritter.newRandomInstance(model);
		Field otherGenomeField = otherCritter.getClass().getDeclaredField("genome");
		otherGenomeField.setAccessible(true);
		BitSet otherGenome = new BitSet(8);
		otherGenome.set(0, 4, true);
		otherGenomeField.set(otherCritter, otherGenome);
		
		model.getRandom().setSeed(1);
		
		BitSetCritter offspring = (BitSetCritter) thisCritter.mateWith(otherCritter);
		Field offspringGenomeField = offspring.getClass().getDeclaredField("genome");
		offspringGenomeField.setAccessible(true);
		BitSet offspringGenome = (BitSet) offspringGenomeField.get(offspring);
		
		System.out.println("offspring: " + offspringGenome.toString());
		
		BitSet result = new BitSet(8);
		result.set(0);
		result.set(1);
		result.set(2);
		result.set(4);
		result.set(5);
		result.set(6);
		assertTrue("Mating did not return expected genome.", offspringGenome.equals(result));
		
		assertEquals("X location of offspring unexpected.", offspring.getLocation()[0], xLoc, 0.0);
		assertEquals("Y location of offspring unexpected.", offspring.getLocation()[1], yLoc, 0.0);
		
		//test dispersal
		thisCritter = BitSetCritter.newRandomInstance(model);
		thisCritter.setLocation(xLoc, yLoc);
		Field dispersalField = model.getClass().getDeclaredField("dispersalProbability");
		dispersalField.setAccessible(true);
		dispersalField.set(model, 1.0d);
		offspring = (BitSetCritter) thisCritter.mateWith(otherCritter);
		
		double distance =   Math.sqrt(((offspring.getLocation()[0] - xLoc) * (offspring.getLocation()[0] - xLoc) + 
							(offspring.getLocation()[1] - yLoc) * (offspring.getLocation()[1] - yLoc)));
		assertTrue("No dispersal of offspring when expected.", distance != 0.0d);
		assertTrue("Distance to dispersed offspring not less than max dispersal distance.", distance <= 1.0d);


	}

	@Test
	public void testMateOpportunistWith() throws Exception {
		double xLoc = 100.0d;
		double yLoc = 100.0d;

		BitSetCritter thisCritter = BitSetCritter.newRandomInstance(model);
		Field thisGenomeField = thisCritter.getClass().getDeclaredField("genome");
		thisGenomeField.setAccessible(true);
		BitSet thisGenome = new BitSet(8);
		thisGenome.set(4, 8, true);
		thisGenomeField.set(thisCritter, thisGenome);
		thisCritter.setLocation(99.0d, 99.0d);

		BitSetCritter otherCritter = BitSetCritter.newRandomInstance(model);
		Field otherGenomeField = otherCritter.getClass().getDeclaredField("genome");
		otherGenomeField.setAccessible(true);
		BitSet otherGenome = new BitSet(8);
		otherGenome.set(0, 4, true);
		otherGenomeField.set(otherCritter, otherGenome);
		
		BitSetCritter earlyDeathCritter = BitSetCritter.newRandomInstance(model);
		earlyDeathCritter.setLocation(xLoc, yLoc);
		
		model.getRandom().setSeed(1);
		
		BitSetCritter offspring = (BitSetCritter) thisCritter.mateOpportunistWith(otherCritter, earlyDeathCritter);
		Field offspringGenomeField = offspring.getClass().getDeclaredField("genome");
		offspringGenomeField.setAccessible(true);
		BitSet offspringGenome = (BitSet) offspringGenomeField.get(offspring);

		System.out.println("offspring: " + offspringGenome.toString());

		BitSet result = new BitSet(8);
		result.set(0);
		result.set(1);
		result.set(2);
		result.set(4);
		result.set(5);
		result.set(6);
		assertTrue("Mating did not return expected genome.", offspringGenome.equals(result));
		
		assertEquals("X location of offspring unexpected.", offspring.getLocation()[0], xLoc, 0.0);
		assertEquals("Y location of offspring unexpected.", offspring.getLocation()[1], yLoc, 0.0);
	}

	@Test
	public void testIsSameSpecies() throws Exception {
		BitSetCritter thisCritter = BitSetCritter.newRandomInstance(model);
		Field thisGenomeField = thisCritter.getClass().getDeclaredField("genome");
		thisGenomeField.setAccessible(true);

		BitSetCritter otherCritter = BitSetCritter.newRandomInstance(model);
		Field otherGenomeField = otherCritter.getClass().getDeclaredField("genome");
		otherGenomeField.setAccessible(true);

		BitSet thisGenome1 = new BitSet(8);
		thisGenomeField.set(thisCritter, thisGenome1);
		BitSet otherGenome1 = new BitSet(8);
		otherGenome1.set(0);
		otherGenomeField.set(otherCritter, otherGenome1);
		assertTrue("Critters are same species.", thisCritter.isSameSpecies(otherCritter, 1));
		
		BitSet thisGenome2 = new BitSet(8);
		thisGenome2.set(0);
		thisGenomeField.set(thisCritter, thisGenome2);
		BitSet otherGenome2 = new BitSet(8);
		otherGenome2.set(0);
		otherGenome2.set(1);
		otherGenome2.set(2);
		otherGenome2.set(5);
		otherGenomeField.set(otherCritter, otherGenome2);
		assertTrue("Critters are same species.", thisCritter.isSameSpecies(otherCritter, 3));

		BitSet thisGenome3 = new BitSet(8);
		thisGenome3.set(0);
		thisGenomeField.set(thisCritter, thisGenome3);
		BitSet otherGenome3 = new BitSet(8);
		otherGenome3.set(3);
		otherGenome3.set(4);
		otherGenome3.set(5);
		otherGenomeField.set(otherCritter, otherGenome3);
		assertTrue("Critters are NOT same species.", !thisCritter.isSameSpecies(otherCritter, 3));
	}
	
	@Test
	public void testCrossover() throws Exception {
		
		BitSetCritter thisCritter = BitSetCritter.newRandomInstance(model);
		Field genome = thisCritter.getClass().getDeclaredField("genome");
		genome.setAccessible(true);
		BitSet thisGenome = new BitSet(8);
		thisGenome.set(4, 8, true);
		genome.set(thisCritter, thisGenome);
		
		//override random generator
		model.getRandom().setSeed(1);
		
		Method crossover = thisCritter.getClass().getDeclaredMethod("crossover", BitSet.class);
		crossover.setAccessible(true);
		BitSet mateGenome = new BitSet(8);
		mateGenome.set(0, 4, true);
		BitSet offspringGenome = (BitSet) crossover.invoke(thisCritter, mateGenome);
		System.out.println("offspring: " + offspringGenome.toString());
		
		BitSet result = new BitSet(8);
		result.set(4);
		assertTrue("Crossover did not return expected genome.", offspringGenome.equals(result));
	}
	
	@Test
	public void testMutation() throws Exception {
		BitSetCritter thisCritter = BitSetCritter.newRandomInstance(model);
		Method mutation = thisCritter.getClass().getDeclaredMethod("mutate", BitSet.class);
		mutation.setAccessible(true);
		
		//override random generator
		model.getRandom().setSeed(1);
		
		BitSet offspring = new BitSet(8); //default is all false
		mutation.invoke(thisCritter, offspring);

		System.out.println("offspring: " + offspring.toString());

		BitSet result = new BitSet(8);
		result.set(1);
		result.set(2);
		result.set(3);
		result.set(5);
		
		assertTrue("Mutation did not return expected genome.", offspring.equals(result));

	}
}
